//
//  WJGiftGroupItemView.m
//  WJAgroManagerDemo
//
//  Created by tqh on 2020/4/7.
//  Copyright © 2020 tqh. All rights reserved.
//

#import "WJGiftGroupItemView.h"
#import "UIView+IM.h"

@interface WJGiftGroupItemView()



@property (nonatomic,strong) UILabel *nameLabel;
@property (nonatomic,strong) UILabel *numberLabel;
@property (nonatomic,assign) NSInteger numberTotal;

@property (nonatomic,strong) UIView *bgView;
@end

@implementation WJGiftGroupItemView
-(void)dealloc {
    NSLog(@"销毁了");
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.removeTime = 3;
 
        [self addSubview:self.bgView];
        [self.bgView addSubview:self.nameLabel];
        [self addSubview:self.numberLabel];
    }
    return self;
}

- (void)layoutSubviews {
    
    CGFloat height = self.height;
    self.numberLabel.frame = CGRectMake(self.width - 40 - 5, 0, 40, height);
}

#pragma makr - public

- (void)resetLocation {

    self.index = 0;
    [self changeLocationWithType:0];
}

- (void)changeLocation {
    self.index++;
    [UIView animateWithDuration:0.5 animations:^{
        [self changeLocationWithType:self.index];
    } completion:^(BOOL finished) {
        if (self.index>=3) {
            [self delayHideControlLayer];
        }
    }];
}

#pragma mark - setter 

- (void)setModel:(WJGiftGroupItemModel *)model {
    _model = model;
    self.nameLabel.text = [NSString stringWithFormat:@"%@发送了【%@】",model.nickName,model.giftName];
    self.numberTotal = model.giftNumber +self.numberTotal;
    self.numberLabel.text = [NSString stringWithFormat:@"X%ld",self.numberTotal];
    CGSize size = [self.nameLabel sizeThatFits:CGSizeMake(375, 40)];
    self.bgView.frame = CGRectMake(0, 0, size.width + 30, self.height);
    self.bgView.layer.cornerRadius = self.height/2;
    self.bgView.layer.masksToBounds = YES;
    self.nameLabel.frame = CGRectMake(15, 0, size.width, self.height);
    self.width = self.bgView.width + 40;
    
    [self p_SetAnimation:self.numberLabel];
    [self startDelayRemoveFunc];
}

#pragma mark - private

- (void)changeLocationWithType:(NSInteger)type {

    CGFloat width = self.width > 0 ? self.width:200;
    CGFloat height = self.height > 0 ? self.height:40;
    
     switch (type) {
         case 0:
             self.frame = CGRectMake(-width, height + self.spaceHeight, width, height);
             break;
         case 1:
             self.frame = CGRectMake(0, height + self.spaceHeight, width, height);
             break;
         case 2:
             self.frame = CGRectMake(0, 0, width, height);
             break;
         case 3:
             self.frame = CGRectMake(0, -height, width, height);
             self.alpha = 0;
             break;
         default:
             break;
     }
    [self startDelayRemoveFunc];
}

- (void)startDelayRemoveFunc {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayHideControlLayer) object:nil];
    [self performSelector:@selector(delayHideControlLayer) withObject:nil afterDelay:self.removeTime];
}

- (void)delayHideControlLayer {
    [self removeFromSuperview];
    if (self.deleteBlock) {
        self.deleteBlock(self);
    }
}


- (void)p_SetAnimation:(UIView *)view {
    
    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.08;
    pulse.repeatCount= 1;
    pulse.autoreverses= YES;
    pulse.fromValue= [NSNumber numberWithFloat:1.0];
    pulse.toValue= [NSNumber numberWithFloat:1.5];
    [[view layer] addAnimation:pulse forKey:nil];
}

#pragma mark - 懒加载

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor redColor];
    }
    return _bgView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.font = [UIFont systemFontOfSize:14];
    }
    return _nameLabel;
}

- (UILabel *)numberLabel {
    if (!_numberLabel) {
        _numberLabel = [UILabel new];
        _numberLabel.textAlignment = NSTextAlignmentRight;
        _numberLabel.textColor = [UIColor orangeColor];
    }
    return _numberLabel;
}

@end
